// 全局公用的js function

/*--- cities select(begin) ---*/
$(document).ready(function(){
	//baseurl = 'http://www.laoa.org/'
	$('.province')
	.change(function(){
		set_cities($(this).val(), $(this).closest('form').find('.city'), '', 1);
	});
	
	$('.city')
	.change(function(){
		set_areas($(this).val(), $(this).closest('form').find('.area'), '');
	});
	
	$('.section_selector').change(function() {
		if ($(this).val() > 0) {
			$.get( '/front/find_sub_categories_ajax', {section_id:$(this).val()}, function(data) {
				var ret = eval('(' + data + ')'); 
				var html = '';
				html += '<option value="0">不限</option>';
				for (var x in ret) {
					html += '<option value="' + ret[x].id + '">' + ret[x].name + '</option>';
				}
				$('#category_id').html(html);
			});
		}
	});
	$('.category_selector').change(function() {
		if ($(this).val() > 0) {
			$.get( '/front/find_sub_categories_ajax', {section_id:$(this).val()}, function(data) {
				var ret = eval('(' + data + ')'); 
				var html = '';
				for (var x in ret) {
					html += '<option value="' + ret[x].id + '">' + ret[x].name + '</option>';
				}
				$('#third_category_id').html(html);
			});
		}
	});
});
function set_cities(province_id, $this, selected, auto_set_area) {
	$.get( '/cities/find_all_cities_ajax', {province_id:province_id}, function(data) {
		var ret = eval('(' + data + ')'); 
		var html = '';
		for (var id in ret) {
			if (id != selected) {
				html += '<option value="' + id + '">' + ret[id] + '</option>';
			} else {
				html += '<option selected value="' + id + '">' + ret[id] + '</option>';
			}
		}
		$this.html(html);
		if (auto_set_area) {
			set_areas($this.val(), $this.closest('form').find('.area'), '');
		}
	});
}

function set_areas(city_id, $this, selected) {
	$.get('/cities/find_all_areas_ajax', {city_id:city_id}, function(data) {
		var ret = eval('(' + data + ')'); 
		var html = '';
		for (var id in ret) {
			if (id != selected) {
				html += '<option value="' + id + '">' + ret[id] + '</option>';
			} else {
				html += '<option selected value="' + id + '">' + ret[id] + '</option>';
			}
		}
		$this.html(html);			
	});
}
/*--- cities select(end)   ---*/